package com.ruoyi.common.redis.configure;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;

/**
 * redis配置
 *
 * @author ruoyi
 */
@Configuration
@EnableCaching
@AutoConfigureBefore(RedisAutoConfiguration.class)
public class RedisConfig extends CachingConfigurerSupport
{
//    @Value("${spring.redis.host}")
//    private String redisHost;
//
//    @Value("${spring.redis.port}")
//    private int redisPort;
//
//    @Value("${spring.redis.password}")
//    private String redisPass;
//
//    @Value("${spring.redis.database}")
//    private int redisDb;
//
//    @Value("${spring.redis.timeout}")
//    private String timeout;

//    @Value("${spring.redis.second.database}")
//    private int redisSecondDatabase;

//    @Bean(name = "redisConnectionFactory")
//    @Primary  //默认选择这个数据源进行执行
//    @Qualifier("redisConnectionFactory")
//    public RedisConnectionFactory redisConnectionFactory() {
//        RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
//        config.setPort(redisPort);
//        config.setHostName(redisHost);
//        config.setDatabase(redisDb);
//        config.setPassword(redisPass);
//        //获得默认的连接池构造
//        //这里需要注意的是，RedisConnectionFactoryJ对于Standalone模式的没有（RedisStandaloneConfiguration，jedisPoolConfig）的构造函数，对此
//        //我们用JedisClientConfiguration接口的builder方法实例化一个构造器，还得类型转换
//        JedisClientConfiguration.DefaultJedisClientConfigurationBuilder jpConfigBuilder = (JedisClientConfiguration.DefaultJedisClientConfigurationBuilder) JedisClientConfiguration.builder();
//        //修改我们的连接池配置
//        jpConfigBuilder.usePooling();
////        jpConfigBuilder.poolConfig(jedisPoolConfig());
//        jpConfigBuilder.readTimeout(Duration.ofMillis(Integer.parseInt(timeout)));
//        jpConfigBuilder.connectTimeout(Duration.ofMillis(Integer.parseInt(timeout)));
//        //通过构造器来构造jedis客户端配置
//        JedisClientConfiguration jedisClientConfiguration = jpConfigBuilder.build();
//        //配置连接池属性
//        return new JedisConnectionFactory(config, jedisClientConfiguration);
//    }

//    @Bean(name = "secondRedisConnectionFactory")
//    @Qualifier("secondRedisConnectionFactory")
//    public RedisConnectionFactory secondRedisConnectionFactory() {
//        RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
//        config.setPort(redisPort);
//        config.setHostName(redisHost);
//        config.setDatabase(redisSecondDatabase);
//        config.setPassword(redisPass);
//        //获得默认的连接池构造
//        //这里需要注意的是，RedisConnectionFactoryJ对于Standalone模式的没有（RedisStandaloneConfiguration，jedisPoolConfig）的构造函数，对此
//        //我们用JedisClientConfiguration接口的builder方法实例化一个构造器，还得类型转换
//        JedisClientConfiguration.DefaultJedisClientConfigurationBuilder jpConfigBuilder = (JedisClientConfiguration.DefaultJedisClientConfigurationBuilder) JedisClientConfiguration.builder();
//        //修改我们的连接池配置
//        jpConfigBuilder.usePooling();
////        jpConfigBuilder.poolConfig(jedisPoolConfig());
//        jpConfigBuilder.readTimeout(Duration.ofMillis(Integer.parseInt(timeout)));
//        jpConfigBuilder.connectTimeout(Duration.ofMillis(Integer.parseInt(timeout)));
//        //通过构造器来构造jedis客户端配置
//        JedisClientConfiguration jedisClientConfiguration = jpConfigBuilder.build();
//        //配置连接池属性
//        return new JedisConnectionFactory(config, jedisClientConfiguration);
//    }
    @Bean
    @SuppressWarnings(value = { "unchecked", "rawtypes" })
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory)
    {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);

        FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class);

        // 使用StringRedisSerializer来序列化和反序列化redis的key值
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(serializer);

        // Hash的key也采用StringRedisSerializer的序列化方式
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(serializer);

        template.afterPropertiesSet();

        return template;
    }
    @Bean
    public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory connectionFactory) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        return container;
    }
}
